home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / WPrefs.app / double.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-29  |  3.9 KB  |  195 lines

  1.  
  2. /*
  3.  * Widget for testing double-clicks
  4.  * 
  5.  */
  6.  
  7. #include "WINGsP.h"
  8.  
  9. #include "double.h"
  10.  
  11.  
  12. typedef struct W_DoubleTest {
  13.     W_Class widgetClass;
  14.     WMView *view;
  15.  
  16.     WMHandlerID timer;
  17.     char on;
  18.     char active;
  19.     char *text;
  20. } _DoubleTest;
  21.  
  22.  
  23.  
  24.  
  25. /* some forward declarations */
  26.  
  27. static void destroyDoubleTest(_DoubleTest *dPtr);
  28. static void paintDoubleTest(_DoubleTest *dPtr);
  29.  
  30.  
  31. static void handleEvents(XEvent *event, void *data);
  32. static void handleActionEvents(XEvent *event, void *data);
  33.  
  34.  
  35.  
  36. /* our widget class ID */
  37. static W_Class DoubleTestClass = 0;
  38.  
  39.  
  40. /*
  41.  * Initializer for our widget. Must be called before creating any
  42.  * instances of the widget.
  43.  */
  44. W_Class
  45. InitDoubleTest(WMScreen *scr)
  46. {
  47.     /* register our widget with WINGs and get our widget class ID */
  48.     if (!DoubleTestClass) {
  49.     DoubleTestClass = W_RegisterUserWidget();
  50.     }
  51.  
  52.     return DoubleTestClass;
  53. }
  54.  
  55.  
  56. /*
  57.  * Our widget fabrication plant.
  58.  */
  59. DoubleTest*
  60. CreateDoubleTest(WMWidget *parent, char *text)
  61. {
  62.     DoubleTest *dPtr;
  63.  
  64.     if (!DoubleTestClass)
  65.     InitDoubleTest(WMWidgetScreen(parent));
  66.     
  67.     /* allocate some storage for our new widget instance */
  68.     dPtr = wmalloc(sizeof(DoubleTest));
  69.     /* initialize it */
  70.     memset(dPtr, 0, sizeof(DoubleTest));
  71.  
  72.     /* set the class ID */
  73.     dPtr->widgetClass = DoubleTestClass;
  74.     
  75.     dPtr->view = W_CreateView(W_VIEW(parent));
  76.     if (!dPtr->view) {
  77.     free(dPtr);
  78.     return NULL;
  79.     }
  80.     /* always do this */
  81.     dPtr->view->self = dPtr;
  82.     
  83.     dPtr->text = wstrdup(text);
  84.  
  85.     WMCreateEventHandler(dPtr->view, ExposureMask /* this allows us to know when we should paint */
  86.              |StructureNotifyMask, /* this allows us to know things like when we are destroyed */
  87.              handleEvents, dPtr);
  88.  
  89.     WMCreateEventHandler(dPtr->view, ButtonPressMask,handleActionEvents, dPtr);
  90.  
  91.     return dPtr;
  92. }
  93.  
  94.  
  95.  
  96. static void
  97. paintDoubleTest(_DoubleTest *dPtr)
  98. {
  99.     W_Screen *scr = dPtr->view->screen;
  100.  
  101.     if (dPtr->active) {
  102.     XFillRectangle(scr->display, dPtr->view->window, WMColorGC(scr->white),
  103.                0, 0, dPtr->view->size.width, dPtr->view->size.height);
  104.     } else {
  105.     XClearWindow(scr->display, dPtr->view->window);
  106.     }
  107.     
  108.     W_DrawRelief(scr, dPtr->view->window, 0, 0, dPtr->view->size.width,
  109.          dPtr->view->size.height, dPtr->on ? WRSunken : WRRaised);
  110.  
  111.     if (dPtr->text) {
  112.     int y;
  113.     y = (dPtr->view->size.height-scr->normalFont->height)/2;
  114.     W_PaintText(dPtr->view, dPtr->view->window, scr->normalFont,  
  115.             dPtr->on, dPtr->on+y, dPtr->view->size.width, WACenter, 
  116.             WMColorGC(scr->black), False, dPtr->text, strlen(dPtr->text));
  117.     }
  118. }
  119.  
  120.  
  121.  
  122. static void
  123. handleEvents(XEvent *event, void *data)
  124. {
  125.     _DoubleTest *dPtr = (_DoubleTest*)data;
  126.  
  127.  
  128.     switch (event->type) {    
  129.      case Expose:
  130.     if (event->xexpose.count!=0)
  131.         break;
  132.     paintDoubleTest(dPtr);
  133.     break;
  134.     
  135.      case DestroyNotify:
  136.     destroyDoubleTest(dPtr);
  137.     break;
  138.     
  139.     }
  140. }
  141.  
  142.  
  143. static void
  144. deactivate(void *data)
  145. {
  146.    _DoubleTest *dPtr = (_DoubleTest*)data;
  147.     
  148.     if (dPtr->active)
  149.     dPtr->active = 0;
  150.     paintDoubleTest(dPtr);
  151.     
  152.     dPtr->timer = NULL;
  153. }
  154.  
  155.  
  156. static void
  157. handleActionEvents(XEvent *event, void *data)
  158. {
  159.     _DoubleTest *dPtr = (_DoubleTest*)data;
  160.     extern _WINGsConfiguration WINGsConfiguration;
  161.  
  162.     switch (event->type) {
  163.      case ButtonPress:
  164.     if (WMIsDoubleClick(event)) {
  165.         if (dPtr->timer)
  166.         WMDeleteTimerHandler(dPtr->timer);
  167.         dPtr->timer = NULL;
  168.         dPtr->on = !dPtr->on;
  169.         dPtr->active = 0;
  170.         paintDoubleTest(dPtr);
  171.     } else {
  172.         dPtr->timer=WMAddTimerHandler(WINGsConfiguration.doubleClickDelay,
  173.                       deactivate, dPtr);
  174.         dPtr->active = 1;
  175.         paintDoubleTest(dPtr);
  176.     }
  177.     break;
  178.     }
  179. }
  180.  
  181.  
  182.  
  183. static void
  184. destroyDoubleTest(_DoubleTest *dPtr)
  185. {
  186.     if (dPtr->timer)
  187.     WMDeleteTimerHandler(dPtr->timer);
  188.     if (dPtr->text)
  189.     free(dPtr->text);
  190.     
  191.     free(dPtr);
  192. }
  193.  
  194.  
  195.